<?php
namespace app\wxapp\controller;

use app\model\Theme; 
use app\model\Worker; 
use app\model\Fans; 
use app\model\Game as Game_mod;

class Game extends Base
{
    public function Game()
    {
global $_P, $_G;
$response = modelShow(40000);
//yxlog($_P);
//yxlog("当前UID:" . $_G['member']['uid']);
if (!$_G['ispost']){
    $response = modelShow(50000);
}else{
    $op = isset($_P['op'])?$_P['op']:'index'; //index默认   order 查找对应的砍价实例   help 帮砍查询 search_bargain
    $uid = empty($_G['member']['uid'])?0:$_G['member']['uid'];
    $themeid = isset($_P['themeid'])?$_P['themeid']:'';
    $from_uid = isset($_P['fromuid'])?$_P['fromuid']:'';

    $params = $_P;
    //基本检测
    $fans = $this->checkuser();
    $system=$_G['system'];
    $theme = Theme::getThemeById($themeid);
    $config =   Theme::getConfig($themeid);
    $worker_info = Fans::getWorkInfo($system,$config,$themeid);
    $workerid = $worker_info['workerid'];

    if ($op=='index'){
        try {
            $error_txt = Game_mod::BaseCheck($theme,$config,$system);
            if (!$error_txt){
                $config =   Theme::getConfig($themeid);
                $isworker = false;
                if ($config['game_out_worker']){
                    //上一级非员工记录
                    $isworker = Worker::getThemeWorkerByUid($from_uid,$themeid,1);
                }
                if ($isworker){
                    $error_txt = modelShow(60000);
                }
            }
            $show_lead = Game_mod::getShowLead($themeid,$uid);//创建初始化第一关进行中
            $swiper_tips = Game_mod::getSwiperTips($themeid,$uid);
            $user_level_check = Game_mod::checkUserLevel($themeid,$uid);
            $game_music = $config['game_musicsrc'];
            $current_game_user_detail_item = Game_mod::getGameUserDetail($themeid,$uid);
            $current_level = $current_game_user_detail_item['current_level'];
            $answer_question = false;
            //判断当前关是否需要回答问题开启
            $next_level = $current_level+1;
            $next_level_info = Game_mod::getGameItemBySort($themeid,$next_level);
            //判断当前关卡记录是否完成
            if ($next_level_info['open_question']){
                $where1 = ['themeid'=>$themeid,'uid'=>$uid,'gl_id'=>$current_level];
                $current_level_user_flag = Game_mod::getGameUserLevelByWhere($where1);
                $where2 = ['themeid'=>$themeid,'uid'=>$uid,'gl_id'=>$next_level];
                $next_level_user_flag = Game_mod::getGameUserLevelByWhere($where2);
                if ($current_level_user_flag['status']=='end'&& $next_level_user_flag['status']=='process'){
                    //判断是否有下一关的问题回答
                    $where3 = ['themeid'=>$themeid,'uid'=>$uid,'gl_id'=>$next_level];
                    $has_next_level_question = Game_mod::checkUserLevelQuestion($where3);
                    if ($has_next_level_question){
                        Game_mod::checkUserLevel($themeid,$uid);
                        $current_game_user_detail_item = Game_mod::getGameUserDetail($themeid,$uid);
                        $current_level = $current_game_user_detail_item['current_level'];
                    }else{
                        $answer_question = true;
                    }
                }
            }
            $current_level_type = $current_game_user_detail_item['current_level_type'];
            $current_level_op_info = Game_mod::getLevelOpNum($current_level_type,$current_game_user_detail_item);
            $total_num = $current_level_op_info['total_num'];
            $need_num = $current_level_op_info['need_num'];
            $remain_num = $current_level_op_info['remain_num'];
            $current_level_prize = Game_mod::getUserCurrentLevelPrize($themeid,$current_level);
            $current_level_prize_info = ['title'=>$current_level_prize['name'],'img'=>$current_level_prize['img']];
            $avatar = $fans['avatar'];
            $RouteAndWaitOpenItem = Game_mod::getUserGameRoute($themeid,$uid,$current_game_user_detail_item);
            $route = $RouteAndWaitOpenItem['route'];
            //带回答问题的宝箱数据集合
            $wait_open_item = $RouteAndWaitOpenItem['wait_open_item'];
            $has_wait_open_box = false;
            if (!empty($wait_open_item)){
                $has_wait_open_box = true;
            }

            $pass_level_tips = $config['pass_level_tips'];

            $pass_level_title = '';
            $pass_level_prize_img = 'https://css2020.yixuan.net/2020css/game/ic19.png';
            $pass_level_content = '';
            $last_level = $current_level-1;
            if ($current_game_user_detail_item['show_last_tips']==1){
                if ($current_level==10){
                    $pass_level = Game_mod::checkUserEndGame($themeid,$uid);
                    if ($pass_level){
                        $last_level_prize = Game_mod::getUserCurrentLevelPrize($themeid,$current_level);
                        $pass_level_title = '太棒了，恭喜亲通过第'.$current_level.'关';
                        if ($last_level_prize['img']){
                            $pass_level_prize_img = $last_level_prize['img'];
                        }
                        $pass_level_content = '获得'.$last_level_prize['name'];
                        $where = ['themeid'=>$themeid,'uid'=>$uid,'gd_id'=>$current_game_user_detail_item['gd_id']];
                        $data = ['show_last_tips'=>-1];
                        $update_user_game_detail = Game_mod::editGameUserDetailByWhere($where,$data);//更新用户闯关成功提示语状态
                    }else{
                        $last_level_prize = Game_mod::getUserCurrentLevelPrize($themeid,$last_level);
                        $pass_level_title = '太棒了，恭喜亲通过第'.$last_level.'关';
                        if ($last_level_prize['img']){
                            $pass_level_prize_img = $last_level_prize['img'];
                        }

                        $pass_level_content = '获得'.$last_level_prize['name'];
                        $where = ['themeid'=>$themeid,'uid'=>$uid,'gd_id'=>$current_game_user_detail_item['gd_id']];
                        $data = ['show_last_tips'=>0];
                        $update_user_game_detail = Game_mod::editGameUserDetailByWhere($where,$data);//更新用户闯关成功提示语状态
                    }
                }else{
                    $last_level_prize = Game_mod::getUserCurrentLevelPrize($themeid,$last_level);
                    $pass_level_title = '太棒了，恭喜亲通过第'.$last_level.'关';
                    if ($last_level_prize['img']){
                        $pass_level_prize_img = $last_level_prize['img'];
                    }

                    $pass_level_content = '获得'.$last_level_prize['name'];
                    $where = ['themeid'=>$themeid,'uid'=>$uid,'gd_id'=>$current_game_user_detail_item['gd_id']];
                    $data = ['show_last_tips'=>0];
                    $update_user_game_detail = Game_mod::editGameUserDetailByWhere($where,$data);//更新用户闯关成功提示语状态
                }
            }
            //宝妈拼拼看跳转五锦拼霸王图标
            $game_to_koi_path = '';
            $game_to_koi_icon = '';
            $five_theme = Game_mod::getThemeListByType('five_koi',$themeid);
            if ($five_theme['type_theme_total']>0){
                $new_theme = Theme::getThemeById($five_theme['type_theme_list'][0]['id']);
                $fanstemp = Fans::getworkerid_fromuid($themeid,$fans);
                $workeridtemp = $fanstemp['workerid'];
                $fromuid = $fanstemp['fromuid'];
                $game_to_koi_path = '/pages/index/index?softname=' . MODULE_NAME . '&themeid=' . $new_theme['id'] . '&softkey=' . $new_theme['softkey']. '&fromuid=' . $fromuid. '&workerid=' . $workeridtemp;
                $game_to_koi_icon = $config['game_to_koi_icon'];
            }

            //客服相关
            $call_kf_mobile_btn = '一键拨号';
//            $worker = [];
//            if (!$workerid){
//                $workerid = Game_mod::getUserInfoById($uid)['workerid'];
//                if ($workerid){
//                    $worker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
//                }
//            }
            $worker= $worker_info;


            $game_method = Game_mod::getGameMethod();
            $shares_box_info = Game_mod::getFriendBoxList($themeid,$uid);

            $swiper_box_level = Game_mod::getSwiperLevelAndBoxInfo($themeid,$uid);
            //共享宝箱订阅消息检测发送
            if (false){
                //未设置page路径  需进入方法设置点击跳转小程序路径
                Game_mod::checkTeamUserAndSendWxMsg($themeid,$uid,$fans,$system);
            }
//            $end_game = Game_mod::checkUserEndGame($themeid,$uid);
//            if ($end_game){
//                $pass_level_title = '';
//            }
            $format_theme = [];
            $format_theme['mobile']=$theme['mobile'];
            $format_res = [
                'error_txt'=>$error_txt,
                'show_lead'=>$show_lead,
                'swiper_tips'=>$swiper_tips,
                'game_music'=>$game_music,
                'current_level'=>$current_level,
                'current_level_type'=>$current_level_type,
                'total_num'=>$total_num,
                'need_num'=>$need_num,
                'remain_num'=>$remain_num,
                'current_level_prize_info'=>$current_level_prize_info,
                'avatar'=>$avatar,
                'route'=>$route,
                'pass_level_tips'=>$pass_level_tips,
                'pass_level_prize_img'=>$pass_level_prize_img,
                'pass_level_title'=>$pass_level_title,
                'pass_level_content'=>$pass_level_content,
                'game_method'=>$game_method,
                'game_to_koi_path'=>$game_to_koi_path,
                'game_to_koi_icon'=>$game_to_koi_icon,
                'kf_qrcode'=>$worker_info['qrcode'],
                'kf_mobile'=>$worker_info['mobile'],
                'worker'=>$worker_info,
                'call_kf_mobile_btn'=>$call_kf_mobile_btn,
                'answer_question'=>$answer_question,
                'has_friend_shares_box'=>$shares_box_info['has_friend_shares_box'],
                'friend_shares_box_info'=>$shares_box_info['friend_shares_box_info'],
                'swiper_box_level'=>$swiper_box_level,
                'has_wait_open_box'=>$has_wait_open_box,
                'wait_open_item'=>$wait_open_item,
                'theme'=>$format_theme
            ];
        }catch (Exception $e){
            var_dump($e->getMessage());die();
        }
        $response = modelShow(200,'',$format_res);
    }elseif ($op=='my_prize'){
        //我的奖品
        try {
            $error_txt = Game_mod::BaseCheck($theme,$config,$system);
            $prize_info = Game_mod::getPrizeInfo($themeid,$uid);
            $has_level_prize = $prize_info['has_level_prize'];
            $has_box_prize = $prize_info['has_box_prize'];
            $level_prize = $prize_info['level_prize'];
            $level_name = (int)$level_prize['level_name'];
            if ($level_name<=10){
                $level_name = '0'.(string)$level_name;
                $level_prize['level_name'] = $level_name;
            }
            $box_prize = $prize_info['box_prize'];
            $convert_method = Game_mod::getConvertMethod($theme,$config);
            $res = [
                'error_txt'=>$error_txt,
                'has_level_prize'=>$has_level_prize,
                'has_box_prize'=>$has_box_prize,
                'level_prize'=>$level_prize,
                'box_prize'=>$box_prize,
                'convert_method'=>$convert_method
            ];
            $response = modelShow(200,'',$res);
        }catch (Exception $e){

        }

    }elseif ($op=='my_life'){
        //成长记录
        $page_index = max(1,intval($params['page']));
        $life_count = Game_mod::getGameLifeCount($themeid,$uid);
        $page_size = 10;
        $page_max = ceil($life_count/$page_size);
        $page_start = ($page_index-1)*$page_size;
        try{
            $error_txt = Game_mod::BaseCheck($theme,$config,$system);
            $current_game_user_detail_item = Game_mod::getGameUserDetail($themeid,$uid);
            $current_level_info = Game_mod::formatCurrentLevelInfo($themeid,$current_game_user_detail_item);
            $life_info = Game_mod::formatLifeInfo($themeid,$uid,$page_start,$page_size);
            $team_info = Game_mod::getTeamFriend($themeid,$uid);
            $shares_box_info_user = Game_mod::formatSharesBoxInfo($themeid,$uid);
            $res = [
                'error_txt'=>$error_txt,
                'current_level_info'=>$current_level_info,
                'shares_box_info'=>$shares_box_info_user,
                'life_info'=>$life_info,
                'page_index'=>$page_index,
                'page_max'=>$page_max,
                'has_team'=>$team_info['has_team'],
                'team_list'=>$team_info['team_list'],
            ];
            $response = modelShow(200,'',$res);
        }catch (Exception $e){
            var_dump($e->getMessage());die();
        }
    }elseif($op=='get_box'){
        //获取随机问题
        $error_txt = Game_mod::BaseCheck($theme,$config,$system);
        $question = Game_mod::formatRandQuestion($themeid);
        $res = [
            'error_txt'=>$error_txt,
            'title'=>$question['title'],
            'kf_qrcode'=>$worker_info['qrcode'],
            'q_id'=>$question['q_id'],
            'selects'=>$question['selects']
        ];
        $response = modelShow(200,'',$res);
    }elseif($op=='level_question'){
        //获取随机问题
        $error_txt = Game_mod::BaseCheck($theme,$config,$system);
        $question = Game_mod::formatRandQuestion($themeid);
        $res = [
            'error_txt'=>$error_txt,
            'title'=>$question['title'],
            'kf_qrcode'=>$worker_info['qrcode'],
            'q_id'=>$question['q_id'],
            'selects'=>$question['selects']
        ];
        $response = modelShow(200,'',$res);
    }elseif ($op=='answer_level_question'){
        $qid = $params['qid'];
        $sid = $params['sid'];
        $error_txt = Game_mod::BaseCheck($theme,$config,$system);
        $format_arr = Game_mod::checkLevelAnswerSure($themeid,$config,$uid,$qid,$sid);
        $res = [
            'error_txt'=>$error_txt,
            'answer_status'=>$format_arr['answer_status'],
            'true_info'=>$format_arr['true_info'],
            'false_info'=>$format_arr['false_info'],
            'selects'=>$format_arr['selects']
        ];
        $response = modelShow(200,'',$res);
    }elseif($op=='open_box'){
        $qid = $params['qid'];
        $sid = $params['sid'];
        $other_id = $params['otherid'];
        $error_txt = Game_mod::BaseCheck($theme,$config,$system);
        $format_arr = Game_mod::checkAnswerSure($themeid,$config,$uid,$qid,$sid,$other_id);
        $res = [
            'error_txt'=>$error_txt,
            'answer_status'=>$format_arr['answer_status'],
            'true_info'=>$format_arr['true_info'],
            'false_info'=>$format_arr['false_info'],
            'selects'=>$format_arr['selects']
        ];
        $response = modelShow(200,'',$res);
    }elseif($op=='upload_share_num'){
        //上报分享动作
        //增加成长记录
        try {
            $error_txt = Game_mod::BaseCheck($theme,$config,$system);
            Game_mod::addUserGameLifeFlag($themeid,$uid,$uid,'share');
        }catch (Exception $e){

        }
        $res = [
            'error_txt'=>$error_txt,
        ];
        $response = modelShow(200,'',$res);
    }elseif ($op=='copy_box_from_friend'){
        $error_txt = Game_mod::BaseCheck($theme,$config,$system);
        $gb_id = $_P['gb_id'];//宝箱id
        $user_last_box_info = Game_mod::getUserLastBox($themeid,$uid);
        $copy_box_prize_info = Game_mod::getGameBoxItemAndPrize($themeid,$gb_id);
        if (!$user_last_box_info){
            //不存在宝箱获取记录
            $data = [
                'themeid'=>$themeid,
                'uid'=>$uid,
                'gb_id'=>$gb_id,
                'p_id'=>$copy_box_prize_info['p_id'],
                'q_id'=>0,
                'remain_friend_view'=>$copy_box_prize_info['friend_view'],
                'is_shares'=>$copy_box_prize_info['type'],
                'is_end'=>$copy_box_prize_info['type']==1?0:1
            ];
            //直接粘贴插入
            $insert_data = Game_mod::insertGameUserBox($data);
        }else{
            //存在宝箱获取记录
            //判断最后一个宝箱获取记录是否与当前复制的宝箱id一致
            if ($user_last_box_info['gb_id']<$gb_id && $user_last_box_info['is_end']){
                $data = [
                    'themeid'=>$themeid,
                    'uid'=>$uid,
                    'gb_id'=>$gb_id,
                    'p_id'=>$copy_box_prize_info['p_id'],
                    'q_id'=>0,
                    'remain_friend_view'=>$copy_box_prize_info['friend_view'],
                    'is_shares'=>$copy_box_prize_info['type'],
                    'is_end'=>$copy_box_prize_info['type']==1?0:1
                ];
                //直接粘贴插入
                $insert_data = Game_mod::insertGameUserBox($data);
            }
        }
        $res = [
            'error_txt'=>$error_txt,
        ];
        $response = modelShow(200,'',$res);
    }
}
wxapp_result(0, '',$response);
}
}